package com.actionpals.utils
{
	import com.actionpals.graphics.Gradient;
	
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	public class ReflectionUtil
	{
		public static function createReflection( source:BitmapData, height:Number, gradient:Gradient ):BitmapData
        {
            var rWidth:Number = source.width;
            var rHeight:Number = height;
            var rY:Number = source.height - rHeight;
            var bitmapData:BitmapData = new BitmapData( rWidth, rHeight, true, 0 );
            var resultData:BitmapData = new BitmapData( rWidth, rHeight, true, 0 );
            var matrix:Matrix = new Matrix();
            
            matrix.d = -1;
            matrix.ty = source.height;
           
            bitmapData.draw( source, matrix );
            resultData.copyPixels(	bitmapData,
            						resultData.rect,
            						new Point(),
            						getAlphaBitmapData( rWidth, rHeight, gradient ),
            						new Point(),
            						true );
            return resultData;
        }
        
		private static function getAlphaBitmapData( width:Number, height:Number, gradient:Gradient ):BitmapData
		{
			var bitmapData:BitmapData;
			var shape:Shape = new Shape();
			
			shape.graphics.beginGradientFill(	gradient.type, 
												gradient.colors, 
												gradient.alphas, 
												gradient.ratios, 
												gradient.matrix, 
												gradient.spreadMethod
											);
												
			shape.graphics.drawRect(0, 0, width, height);
			shape.graphics.endFill();
		 
			bitmapData = new BitmapData( shape.width, shape.height, true, 0 );
			bitmapData.draw( shape );

			return bitmapData;
		}
	}
}